package com.yonyou.crm.cum.customer.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.model.util.ModelUtil;
import com.yonyou.crm.cum.customer.entity.CustomerFollowDO;
import com.yonyou.crm.cum.customer.entity.CustomerFollowVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class CustomerFollowBizServiceImpl extends BaseBizServiceImpl<CustomerFollowDO> {
	
	public int isFollow(Long cumId) {
		String userIdStr = (String)InvocationInfoProxy.getParameter("id");//用户id
		String orgIdStr = (String)InvocationInfoProxy.getParameter("orgid");//所属公司
		String tenantIdStr = (String)InvocationInfoProxy.getTenantid();
		Long userId = Long.parseLong(userIdStr);
		Long orgId = Long.parseLong(orgIdStr);
		Long tenantId = Long.parseLong(tenantIdStr);
		Map<String, Object> paramMap = new HashedMap<String, Object>();
		paramMap.put("userId", userId);
		paramMap.put("orgId", orgId);
		paramMap.put("tenantId", tenantId);
		paramMap.put("cumId", cumId);
		int count = dao.countAll(CustomerFollowDO.class, "isFollow", paramMap);
		if (count > 0) {
			return 1;
		}
		return 0;
	}
	
	public CustomerFollowVO insert(CustomerFollowVO flwVO){
		CustomerFollowDO flwDO = ModelUtil.copyModel(flwVO, CustomerFollowDO.class);
		flwVO = ModelUtil.copyModel(super.saveVO(flwDO), CustomerFollowVO.class);
		return flwVO;
	}
	
	@Override
	public void beforeSaveVO(CustomerFollowDO flwDO) {
		int isFollow = isFollow(flwDO.getCumId());
		if (isFollow >= 1) {
			return;
		}
		super.beforeSaveVO(flwDO);

		String userIdStr = (String)InvocationInfoProxy.getParameter("id");//用户id
		String deptIdStr = (String)InvocationInfoProxy.getParameter("deptid");//所属部门
		String orgIdStr = (String)InvocationInfoProxy.getParameter("orgid");//所属公司
		Long userId = Long.parseLong(userIdStr);
		Long deptId = Long.parseLong(deptIdStr);
		Long orgId = Long.parseLong(orgIdStr);
		Date date = new Date();
		flwDO.setOrgId(orgId);
		flwDO.setDeptId(deptId);
		flwDO.setUserId(userId);
		flwDO.setCreatedTime(date);
	}
	
	public int delete(CustomerFollowVO flwVO) {
		CustomerFollowDO flwDO = ModelUtil.copyModel(flwVO, CustomerFollowDO.class);
		String userIdStr = (String)InvocationInfoProxy.getParameter("id");//用户id
		String deptIdStr = (String)InvocationInfoProxy.getParameter("deptid");//所属部门
		String orgIdStr = (String)InvocationInfoProxy.getParameter("orgid");//所属公司
		String tenantIdStr = (String)InvocationInfoProxy.getTenantid();
		Long userId = Long.parseLong(userIdStr);
		Long deptId = Long.parseLong(deptIdStr);
		Long orgId = Long.parseLong(orgIdStr);
		Long tenantId = Long.parseLong(tenantIdStr);
		flwDO.setUserId(userId);
		flwDO.setDeptId(deptId);
		flwDO.setOrgId(orgId);
		flwDO.setTenantId(tenantId);
		return dao.deleteSql(CustomerFollowDO.class, "delete", flwDO);
	}
	
	public int batchDeleteByCumIds(String[] cumIds) {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("cumIds", cumIds);
		return dao.deleteSql(CustomerFollowDO.class, "batchDeleteByCumIds", paraMap);
	}
	
}
